package com.linkhub.oms.admin.modules.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.linkhub.oms.admin.modules.order.domain.param.*;
import com.linkhub.oms.admin.modules.order.domain.param.orderproduct.OrderLocalSkuDeleteParam;
import com.linkhub.oms.admin.modules.order.domain.param.orderproduct.OrderLocalSkuReplaceParam;
import com.linkhub.oms.admin.modules.order.domain.param.orderproduct.OrderLocalSkuUpdateParam;
import com.linkhub.oms.admin.modules.order.domain.param.ordersplit.OrderSplitParam;
import com.linkhub.oms.admin.modules.order.domain.param.ordersplit.OrderSplitRestoreParam;
import com.linkhub.oms.admin.modules.order.domain.vo.ManualOrderImportResultVO;
import com.linkhub.oms.admin.modules.order.domain.vo.OrderFulfillmentResultVO;
import com.linkhub.oms.admin.modules.order.domain.vo.OrderTabCountVO;
import com.linkhub.oms.admin.modules.order.domain.vo.SyncOrderVO;
import com.linkhub.oms.admin.modules.order.domain.vo.orderdetail.OrderDetailVO;
import com.linkhub.oms.admin.modules.order.domain.vo.orderlist.OrderlistVO;
import com.linkhub.oms.admin.modules.order.service.CustomerOrderBizService;
import com.linkhub.oms.admin.modules.order.service.OrderBizService;
import com.linkhub.oms.admin.modules.order.service.OrderInfoAssembleService;
import com.linkhub.oms.admin.modules.order.service.OrderProductBizService;
import com.linkhub.oms.admin.modules.order.util.OrderMappingAssist;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.response.PageDTO;
import com.linkhub.oms.entity.resulttype.order.OrderInfoDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description: CustomerOrderController
 * @Author: echo
 * @Date: 2024/3/29 10:54
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"客户订单管理模块"})
@RequestMapping("/order/customer")
@SaCheckLogin
@SaCheckRole(value = {RoleConstant.CUSTOMER})
public class CustomerOrderController {

    @Resource
    private CustomerOrderBizService customerOrderBizService;
    @Resource
    private OrderProductBizService orderProductBizService;
    @Resource
    private OrderInfoAssembleService orderInfoAssembleService;
    @Resource
    private OrderBizService orderBizService;

    @GetMapping("/list")
    @ApiOperation(value = "订单列表查询接口")
    public DataResponse<PageDTO<OrderlistVO>> pageCustomerOrders(@Validated CustomerOrderQueryParam orderQueryParam) {
        return DataResponse.success(customerOrderBizService.pageCustomerOrders(orderQueryParam));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "订单详情接口")
    public DataResponse<OrderDetailVO> orderDetail(@Validated BaseSingleOrderParam orderParam) {
        OrderInfoDO orderInfoDO = orderInfoAssembleService.assembleOrder(orderParam.getSysOrderId());
        return DataResponse.success(OrderMappingAssist.mappingOrderDetail(orderInfoDO));
    }

    @PostMapping("/sync")
    @ApiOperation(value = "同步订单接口")
    public DataResponse<SyncOrderVO> syncOrders() {
        return DataResponse.success(customerOrderBizService.manualUpdateShopOrders(LhSessionContext.getUserId()));
    }

    @PostMapping("/address")
    @ApiOperation(value = "订单地址更新接口")
    public DataResponse<Void> updateOrderAddress(@Valid @RequestBody OrderAddressUpdateParam addressUpdateParam) {
        customerOrderBizService.updateOrderAddress(addressUpdateParam);
        return DataResponse.success();
    }

    @PutMapping
    @ApiOperation(value = "订单主表信息更新接口")
    public DataResponse<Void> updateOrder(@Valid @RequestBody OrderInfoUpdateParam orderInfoUpdateParam) {
        customerOrderBizService.updateOrderInfo(orderInfoUpdateParam);
        return DataResponse.success();
    }

    @PostMapping("/product")
    @ApiOperation(value = "新增订单本地商品接口")
    public DataResponse<Void> saveOrderLocalProduct(@Valid @RequestBody OrderLocalSkuUpdateParam skuUpdateParam) {
        orderProductBizService.saveOrderLocalProduct(skuUpdateParam);
        return DataResponse.success();
    }

    @PutMapping("/product")
    @ApiOperation(value = "更新订单本地商品数量接口")
    public DataResponse<Void> updateOrderLocalProduct(@Valid @RequestBody OrderLocalSkuUpdateParam skuUpdateParam) {
        orderProductBizService.updateOrderLocalProduct(skuUpdateParam);
        return DataResponse.success();
    }

    @PutMapping("/product/replace")
    @ApiOperation(value = "替换订单本地商品接口")
    public DataResponse<Void> replaceOrderLocalProduct(@Valid @RequestBody OrderLocalSkuReplaceParam skuReplaceParam) {
        orderProductBizService.replaceOrderLocalProduct(skuReplaceParam);
        return DataResponse.success();
    }

    @DeleteMapping("/product")
    @ApiOperation(value = "删除订单商品接口")
    public DataResponse<Void> deleteOrderProduct(@Valid @RequestBody OrderLocalSkuDeleteParam skuDeleteParam) {
        orderProductBizService.deleteOrderProduct(skuDeleteParam);
        return DataResponse.success();
    }

    @PostMapping("/resolution")
    @ApiOperation(value = "订单拆分接口")
    public DataResponse<Void> splitOrder(@Valid @RequestBody OrderSplitParam orderSplitParam) {
        customerOrderBizService.splitOrder(orderSplitParam);
        return DataResponse.success();
    }

    @PostMapping("/restore")
    @ApiOperation(value = "还原订单接口")
    public DataResponse<Void> restoreOrder(@Valid @RequestBody OrderSplitRestoreParam restoreParam) {
        customerOrderBizService.restoreOrder(restoreParam);
        return DataResponse.success();
    }

    @GetMapping("/tab/count")
    @ApiOperation(value = "订单页签数量接口")
    public DataResponse<List<OrderTabCountVO>> getTabOrderCount(@Validated CustomerOrderCountQueryParam orderCountQueryParam) {
        orderCountQueryParam.setSysShopIds(LhSessionContext.getUserShopIds());
        return DataResponse.success(customerOrderBizService.queryOrderCount(orderCountQueryParam));
    }

    @PutMapping("/state")
    @ApiOperation(value = "订单业务状态变更接口", notes = "移入待审核、审核通过、作废订单、搁置订单、还原订单")
    public DataResponse<Void> changeOrderBizState(@Valid @RequestBody OrderBizStatusChangeParam statusChangeParam) {

        statusChangeParam.setCustomerRole(true);
        orderBizService.changeBizState(statusChangeParam);
        return DataResponse.success();
    }

    @PostMapping("/fulfillment")
    @ApiOperation(value = "单个/批量提交履约接口")
    public DataResponse<OrderFulfillmentResultVO> submitFulfillment(@Valid @RequestBody OrderFulfillmentParam orderFulfillment) {
        return DataResponse.success(customerOrderBizService.submitOrderFulfillments(orderFulfillment));
    }

    @PostMapping("/revive/process")
    @ApiOperation(value = "移入待处理接口")
    public DataResponse<Void> reviveProcess(@Valid BaseSingleOrderParam singleOrderParam) {
        customerOrderBizService.reviveProcess(singleOrderParam);
        return DataResponse.success();
    }

    @PostMapping("/manual/create")
    @ApiOperation(value = "创建手工订单接口")
    public DataResponse<Void> manualCreate(@RequestBody @Valid ManualCreateOrderParam manualCreateOrderParam) {
        customerOrderBizService.manualCreate(manualCreateOrderParam);
        return DataResponse.success();
    }

    @PostMapping(value = "/manual/import")
    @ApiOperation(value = "手工订单excel导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "手工订单excel文件", required = true,
                    dataType = "MultipartFile", dataTypeClass = MultipartFile.class, paramType = "form")
    })
    public DataResponse<ManualOrderImportResultVO> importManualOrders( @RequestParam("file") MultipartFile manualOrderExcel) {
        String userId = LhSessionContext.getUserId();
        return DataResponse.success(customerOrderBizService.importManualOrders(userId, manualOrderExcel));
    }
}
